package com.mairuide.common.phone;

import com.mairuide._frame.utils.Testtrans;
import com.mairuide.entity.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-06-26 17:20
 *  说明：该类是话单详情类，仅支持话单详情，自己需要啥条件自己去实体类加，一定要加上详细的注释。不要影响其他人。
 */
@Service
public class PhoneRecordingDetailService {
    @PersistenceContext
    private EntityManager entityManager;
    //查询话单详情  公共方法
    public PageResult search_detail(PhoneSearchParam dto) {
        String service_number = dto.getServiceNumber();
        String caseId = dto.getCaseId();
        Integer callType = dto.getCallType();
        String activeCity = dto.getActiveCity();
        String idNumber = dto.getIdNumber();
        String serviceUserInformation = dto.getServiceUserInformation();
        String partyLocation = dto.getPartyLocation();
        String beginTime = dto.getBeginTime();
        String endTime = dto.getEndTime();
        String passiveCity = dto.getPassiveCity();
        String order = dto.getOrder();
        String sort = dto.getSort();
        String partyNumber = dto.getPartyNumber();
        String ct_beginTime = dto.getCt_beginTime();
        String ct_endTime = dto.getCt_endTime();

        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT a.* ,STARTING_TIME startingTime ,TALK_TIME talkTime ");
        sb.append(" from TB_PHONE_RECORDING a WHERE 1=1 ");
        if (StringUtils.isNotBlank(caseId)){ // 案件id
            sb.append(" and  CASEMANAGE_ID = '" + caseId +"'");
        }else{
            sb.append(" and  1=0 ");
        }
        if (callType !=null){ // 呼叫类型
            sb.append(" and CALL_TYPE = " + callType) ;
        }
        if (StringUtils.isNotBlank(idNumber)){ // 身份证
            sb.append(" and ID_NUMBER = '" + idNumber +"' ") ;
        }
        if (StringUtils.isNotBlank(serviceUserInformation)){ // 服务号码姓名
            sb.append(" and SERVICE_USER_INFORMATION = '" + serviceUserInformation +"'");
        }
        if (StringUtils.isNotBlank(partyNumber)){ // 对方号码
            sb.append(" and PARTY_NUMBER in (" + partyNumber +") ");
        }
        if (StringUtils.isNotBlank(partyLocation)){ // 对方号码归属地
            sb.append(" and PARTY_LOCATION like '%" + partyLocation +"%'");
        }
        if (StringUtils.isNotBlank(beginTime)){ // 开始时间
            sb.append(" and  to_char(STARTING_TIME,'yyyy-mm-dd') >= '" + beginTime +"'");
        }
        if (StringUtils.isNotBlank(endTime)){ // 结束时间
            sb.append(" and  to_char(STARTING_TIME,'yyyy-mm-dd') <= '" + endTime +"'");
        }
        if (StringUtils.isNotBlank(activeCity)){//主叫城市
            sb.append(" and SERVICE_LOCATION like '%" + activeCity +"%'");
        }
        if (StringUtils.isNotBlank(passiveCity)){//被叫城市
            sb.append(" and PARTY_LOCATION like '%" + passiveCity +"%'");
        }
        if (StringUtils.isNotBlank(service_number)){ // 电话号码集合
            sb.append(" and SERVICE_NUMBER in (" + service_number +")");
        }
        //穿透分析需要的条件--------------
        if (StringUtils.isNotBlank(ct_beginTime)){ // 开始时间
            sb.append(" and  to_char(STARTING_TIME,'yyyy-mm-dd hh24:mi:ss') >= '" + ct_beginTime +"'");
        }
        if (StringUtils.isNotBlank(ct_beginTime)){ // 结束时间
            sb.append(" and  to_char(STARTING_TIME,'yyyy-mm-dd hh24:mi:ss') <= '" + ct_endTime +"'");
        }

        if (StringUtils.isNotBlank(sort)) {
            sb.append(" order by " + sort + " " + order);
        }else{
            sb.append(" ORDER BY STARTING_TIME DESC");
        }


        Query query = this.entityManager.createNativeQuery(sb.toString());
        //将查询结果集转为Map
        query.unwrap(SQLQuery.class).setResultTransformer(new Testtrans(PhoneRecordDeatil.class));
        List<PhoneRecordDeatil> maps = query.getResultList();

        query.setFirstResult((dto.getPage()-1)*dto.getRows());
        query.setMaxResults(dto.getRows());
        //获取查询结果集
        List<PhoneRecordDeatil> list = query.getResultList();
        PageResult pageResult = new PageResult(Long.valueOf(maps.size()),list,null);
        return pageResult;
    }
}
